import 'package:basic_common/app_theme.dart';
import 'package:basic_common/bottom_sheet_tool.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_widgets/button_widget.dart';
import 'package:basic_widgets/input_widget.dart';
import 'package:basic_widgets/text_widget.dart';
import '../realname_controller.dart';

class RealNameView extends GetView<RealNameController> {
  const RealNameView({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenHelper.screenHeight - ScreenHelper.height(100),
      child: Column(
        children: [
          Container(
            color: Colors.white,
            child: ListView(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              children: [
                buildInputInfoItem(
                    attrName: "姓名",
                    arrowChild: inputBoxNoBorder(
                        hint: '请填写您的真实姓名',
                        textInputAction: TextInputAction.next,
                        inputController: controller.nameController)),
                divider(),
                buildInputInfoItem(
                    attrName: "身份证号",
                    arrowChild: inputBoxNoBorder(
                        hint: '请填写您的身份证号',
                        textInputAction: TextInputAction.go,
                        focusNode: controller.focusNode,
                        onSubmitted: (p0) {
                          controller.focusNode.unfocus();
                        },
                        onEditingComplete: () {
                          FocusScope.of(context)
                              .requestFocus(controller.focusNode);
                        },
                        inputController: controller.idCardController)),
                divider(),
                buildInputInfoItem(
                    attrName: "性别",
                    arrowChild: Padding(
                      padding: EdgeInsets.only(right: ScreenHelper.width(30)),
                      child: Obx(() => Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              GestureDetector(
                                  onTap: () {
                                    controller.sex.value = '1';
                                  },
                                  child: buildGenderCheck(
                                      title: "男",
                                      isChecked: controller.sex.value == '1'
                                          ? true
                                          : false)),
                              SizedBox(
                                width:
                                    ScreenHelper.width(ScreenHelper.width(60)),
                              ),
                              GestureDetector(
                                  onTap: () {
                                    controller.sex.value = '0';
                                  },
                                  child: buildGenderCheck(
                                      title: "女",
                                      isChecked: controller.sex.value == '0'
                                          ? true
                                          : false))
                            ],
                          )),
                    )),
                divider(),
                Obx(() => buildInputInfoItem(
                    attrName: "出生年月",
                    arrowChild: selectItem(
                        text: controller.birthday.value,
                        textColor: controller.birthday.value.contains("请输入")
                            ? AppTheme.hintTextColor
                            : AppTheme.dartTextColor),
                    onTap: () {
                      BottomSheetTool.datePickerView(onConfirm: (p) {
                        controller.birthday.value = p.year.toString() +
                            "-" +
                            p.month.toString() +
                            "-" +
                            p.day.toString();
                      });
                    })),
                divider(),
                // buildInputInfoItem(
                //     attrName: "邮箱",
                //     isRequired: false,
                //     arrowChild: inputBoxNoBorder(
                //         hint: '请输入正确的邮箱',
                //         inputController: controller.emailController)),
              ],
            ),
          ),
          SizedBox(
            height: ScreenHelper.height(10),
          ),
          Expanded(child: _buildIdentityWidget()),
        ],
      ),
    );
  }


  /// 身份证照片
  Widget _buildIdentityWidget() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
      // margin: EdgeInsets.only(top: 10),
      child: Column(
        children: [
          // norTitle(title: '身份证照片'),
          buildInputInfoItem(
              arrowChild: SizedBox.shrink(),
              attrName: '身份证照片',
              padding: 0,
              isRequired: false),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              GestureDetector(
                onTap: () {
                  controller.showSelectImg(type: 1);
                },
                child: GetBuilder<RealNameController>(
                    id: 'imgPath1',
                    builder: (context) {
                      return _identityItemWidget(
                          webImg: controller.imgPath1.value,
                          defaultImg: "assets/register/img_card_front.png",
                          textInfo: "拍摄身份证正面");
                    }),
              ),
              SizedBox(
                width: ScreenHelper.width(5),
              ),
              GestureDetector(
                onTap: () => controller.showSelectImg(type: 2),
                child: GetBuilder<RealNameController>(
                  id: 'imgPath2',
                  builder: (controller) => _identityItemWidget(
                      webImg: controller.imgPath2.value,
                      defaultImg: "assets/register/img_card_back.png",
                      textInfo: "拍摄身份证反面"),
                ),
              )
            ],
          ),
          Expanded(child: bottomInfoWidget())
        ],
      ),
    );
  }

  /// 身份证信息上传Item
  Widget _identityItemWidget(
      {required defaultImg, required String webImg, required String textInfo}) {
    return Column(
      children: [
        Container(
          width: ScreenHelper.width(162),
          height: ScreenHelper.height(110),
          color: AppTheme.appBgColor,
          alignment: Alignment.center,
          child: webImg.isEmpty
              ? Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      defaultImg,
                      width: ScreenHelper.width(122),
                      height: ScreenHelper.height(82),
                    )
                  ],
                )
              : Image.network(
                  webImg,
                  fit: BoxFit.fill,
                  height: ScreenHelper.height(110),
                ),
        ),
        Container(
          width: ScreenHelper.width(162),
          padding: EdgeInsets.symmetric(vertical: ScreenHelper.height(8)),
          decoration: const BoxDecoration(
              color: AppTheme.primyColor,
              borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(10),
                  bottomRight: Radius.circular(10))),
          child: Text(
            textInfo,
            textAlign: TextAlign.center,
            style:
                TextStyle(color: Colors.white, fontSize: ScreenHelper.sp(14)),
          ),
        )
      ],
    );
  }

// 底部文字与按钮
  Widget bottomInfoWidget() {
    return Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.only(top: ScreenHelper.height(30)),
      child: Column(
        children: [
          Text.rich(
            TextSpan(
                style: TextStyle(
                  fontSize: ScreenHelper.sp(12),
                  color: AppTheme.weakTextColor,
                ),
                children: [
                  TextSpan(
                      text: "依据相关法律规定，医疗机构应当开展互联网诊疗活动的医疗人员进行实名认证，请您阅读并同意",
                      style: TextStyle(
                          color: AppTheme.seconddarkWeakTextColor,
                          fontSize: ScreenHelper.sp(14))),
                  textLink("《服务协议》", type: 1),
                  TextSpan(
                      text: "与",
                      style: TextStyle(
                          color: AppTheme.seconddarkWeakTextColor,
                          fontSize: ScreenHelper.sp(13))),
                  textLink("《隐私政策》", type: 1),
                ]),
            textAlign: TextAlign.left,
          ),
          SizedBox(height: ScreenHelper.height(10)),
          Expanded(
            child: roundBtn(
                width: ScreenHelper.screenWidth,
                isEnable: true,
                onTap: () {
                  controller.addDrRealNAme();
                  controller.nickNameController.text =
                      controller.nameController.text;
                },
                textColor: Colors.white,
                text: '已阅读并同意，提交实名认证'),
          ),
        ],
      ),
    );
  }
}
